package com.mew.chess.core.game.bjl;

import cn.hutool.core.date.DateUnit;
import com.mew.chess.core.common.handler.AbstractEnterExitHandler;
import com.mew.chess.core.common.handler.ChessEEAnnotation;
import com.mew.chess.core.common.message.request.EnterGameRequestMessage;
import com.mew.chess.core.game.bjl.game.BjlRoomManager;
import com.mew.chess.core.game.bjl.game.BjlGameManager;
import com.mew.chess.core.game.bjl.game.common.BjlConstantValue;
import com.mew.chess.core.game.bjl.game.message.EnterResponseMessage;
import com.mew.chess.core.game.bjl.game.message.UserMessage;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.mew.common.rest.bo.MewSession;
import com.mew.common.rest.game.ChessEnterRoomResponse;
import com.mew.common.util.DateUtils;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * 进入退出游戏Handler
 */
@ChessEEAnnotation("BJL")
@Service
@Slf4j
public class BjlEEHandler extends AbstractEnterExitHandler {

    /**
     * 坐下 准备进入房间
     */
    @Override
    public ResponseCode doSitDown(int roomId, GameSession chessSession, MewSession session, boolean isRepeat) {
        //1 校验
        BjlRoomManager roomManager = BjlGameManager.getInstance().getController(roomId);
        if (null == roomManager){
            log.error("BjlRoomManager roomId {} is null", roomId);
            return ResponseCode.SysException;
        }
        if (roomManager.getOnlineUser().size() > BjlConstantValue.MAX_USER){
            log.error("BjlRoomManager roomId {} room user is full", roomId);
            return ResponseCode.SysException;
        }
        return ResponseCode.Success;
    }

    /**
     * 进入房间完毕
     * @param enterRoomResponse
     */
    @Override
    public void doFinish(GameSession chessSession, ZebraCommand command, ChessEnterRoomResponse enterRoomResponse) {
        BjlRoomManager roomManager = BjlGameManager.getInstance().getController(chessSession.getRoomId());
        UserMessage userVo = roomManager.registerOnlineUser(chessSession);

        //2 当有一人坐下 游戏就开始
        if (!roomManager.checkIsNoStart()){
            roomManager.startGame();
        }

        EnterResponseMessage responseMessage = new EnterResponseMessage();
        responseMessage.setUserMessage(userVo);
        responseMessage.setRoomBetArray(roomManager.getBetArrays());
        responseMessage.setLastBetNumber(enterRoomResponse.getLastBetNumber());
        responseMessage.setMaxBetLimit(roomManager.getMaxBetLimit());

        responseMessage.setLinkType(roomManager.getLinkType());
        responseMessage.setRemainTime(DateUtils.remainTimeMillis(roomManager.getStartTime() == 0 ? System.currentTimeMillis() : roomManager.getStartTime(), roomManager.getRuleTime()));

        responseMessage.setIno(roomManager.getIno());
        responseMessage.setCardNumber(roomManager.getCardLib().size());
        responseMessage.setTypebetNumber(roomManager.getTypeNumber());
        responseMessage.setBankerCards(roomManager.getBjlResult().getBankerCards());
        responseMessage.setIdleCards(roomManager.getBjlResult().getIdleCards());
        responseMessage.setHistoryVOList(roomManager.getHistoryList());
        responseMessage.setBigRList(roomManager.getBigRList().values());

        log.debug("进入游戏 {} ",responseMessage.toString());
        command.setToken(chessSession.getTk());
        command.sendMessage(responseMessage);

    }

    /**
     * 退出游戏
     * @param roomId
     */
    @Override
    public boolean doExitRoom(MewSession session, int roomId) {
        if(!session.checkChessPlaying()) {
            BjlRoomManager roomManager = BjlGameManager.getInstance().getController(roomId);
            roomManager.exitRoom(session.uid);
            return false;
        }
        return true;
    }


}
